package com.yutel.google;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.com.xinli.android.service.protocol.DataSearch;
import cn.com.xinli.android.service.protocol.DataSearch.Attribute;
import cn.com.xinli.android.service.protocol.DataSearch.SearchResp.SearchResult;

public class TestDataSearch {
	private String AUTHIPADRESS="192.168.2.243";
	private int connTimeOut=20000;
	private int dataTimeOut=60000;
	public static void main(String[] arg) {
		ArrayList<String[]> l=new ArrayList<String[]>();
		l.add(new String[]{"channel","电视剧"});
		//l.add(new String[]{"category","内地剧场"});
		new TestDataSearch().testSearchReq(l,0,100);
	}
	public void testSearchReq(ArrayList<String[]> filterinfo,int begin,int count) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput=null;
		DataSearch.SearchReq request=null;
		int response=1;
		try {
    		socketClient=new Socket();
    		socketClient.connect(new InetSocketAddress(AUTHIPADRESS,5252), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput=new DataOutputStream(socketClient.getOutputStream());
			DataSearch.SearchReq.Builder builder=DataSearch.SearchReq.newBuilder();
			builder.setStart(begin);
			builder.setCount(count);
			for(int  i=0;i<filterinfo.size();i++){
				String filter[]=(String[])filterinfo.get(i);
				if(filter!=null&&filter.length>0){
					String type=filter[0];
					String value=filter[1];
					DataSearch.Attribute.Builder attributebuilder=DataSearch.Attribute.newBuilder();
					attributebuilder.setType(type);
					attributebuilder.setValue(value);
					DataSearch.Attribute attrbuAttribute=attributebuilder.build();
					builder.addFilter(attrbuAttribute);

				}
			}
			request=builder.build();			
			dataOutput.writeInt(request.getSerializedSize());//传输序列化的字节长度
			dataOutput.write(request.toByteArray());//将发送内容转换为字节码数组
			dataOutput.flush();//清空缓存
			InputStream in=socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length=dataInput.readInt();//获取返回值的长度
			System.out.println("lenth="+length);
		    byte dataini[]=new byte[length];//设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			DataSearch.SearchResp searchResponse=DataSearch.SearchResp.parseFrom(dataini);
			System.out.println("getResultCode="+searchResponse.getResultCode());
			System.out.println("getResultMessage="+searchResponse.getResultMessage());
			System.out.println("getResultLeft="+searchResponse.getResultLeft());
			System.out.println("getFilterList=\n"+searchResponse.getFilterList());
			List<SearchResult> resultlistList= searchResponse.getResultList();
			System.out.println("size="+resultlistList.size());
			for(SearchResult sr:resultlistList) {
				List<Attribute> l=sr.getAttrList();
				for(Attribute a:l) {
					System.out.print(":type="+a.getType()+",value="+a.getValue());
				}
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
